package com.arvato.wxwork.controller.front.ncc;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.arvato.common.util.JsonUtil;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.wxwork.bean.bean_do.ncc.WxworkTaskDo;
import com.arvato.wxwork.bean.bean_vo.ncc.WxworkTaskVo;
import com.arvato.wxwork.service.ncc.WxworkNccTaskService;

/**
 * 资生堂NCC(客户关怀) 处理BC需发送任务相关逻辑的Controller
 * @author ZHAN655
 */
@RestController
@RequestMapping("/wxwork/ncc/task")
public class WxworkNccTaskController{
	
	private Logger logger = LoggerFactory.getLogger(WxworkNccTaskController.class);
	
	@Autowired
	private WxworkNccTaskService wxworkNccTaskService; 
	
	@Autowired
	private JsonUtil jsonUtil;
	/**
	 * 同步NCC任务列表
	 * @param req
	 * @return
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping(value="/async")
	public Result list(HttpServletRequest req) throws IOException {
		logger.info("调用NCC接口");
		ServletInputStream is=req.getInputStream();
		int nRead=1;
		int nTotalRead=0;
		byte[] bytes=new byte[10240];
		while(nRead>0) {
			nRead=is.read(bytes,nTotalRead,bytes.length-nTotalRead);
			if(nRead>0) {
				nTotalRead=nTotalRead+nRead;
			} 
		}
		String str=new String(bytes,0,nTotalRead,"utf-8");
		System.out.println(str);
		Map<String, Object> resultMap = jsonUtil.str2Map(str);
		Map<String, Object> data = wxworkNccTaskService.getTaskList(resultMap.get("Date").toString(), resultMap.get("CampaignID").toString(), 1, 20);
		// 抽出数据之后 要和绑定关系表里面数据做匹配 得出需要给那个
		return Result.success(data);
	}
	 
	@ResponseBody
	@RequestMapping(value="/update")
	public Result updateTask(String id) {
		logger.info("调用updateTask接口");
		String[] arr = id.split(",");
		int total=0;
		for(String taskStr : arr) {
			WxworkTaskDo taskDo = new WxworkTaskDo();
			String taskId = taskStr.split("&")[0];
			String greetId = taskStr.split("&")[1];
			taskDo.setId(taskId);
			taskDo.setGreetId(greetId);
			taskDo.setStatus("1");
			int row=wxworkNccTaskService.update(taskDo);
			if(row == 1) {
				total ++;
			}
		}
		if(total == arr.length) {
			return Result.success("操作成功");
	    }
		return Result.success(total);
	}	
	
	 /**
     * Task列表
     * @param staffId
     * @return
     */
	@ResponseBody
	@RequestMapping(value="/list")
	public Result taskListByStaffId(String staffId, String showOn) {
		logger.info("调用taskListByStaffId接口");
		Map<String,List<WxworkTaskVo>> map=new HashMap<String,List<WxworkTaskVo>>();
		Query query27nutm = new Query();
		query27nutm.put("staffId", staffId);
		query27nutm.put("showOn", showOn);
		query27nutm.put("queryId", "51575832709d-3243-2d2d-ad71-4a9540d406ef");
		query27nutm.put("campaignid", "5157");
		query27nutm.put("type", "BATCH");
		List<WxworkTaskVo> nutm27list=wxworkNccTaskService.taskListByStaffId(query27nutm);
		
		Query query27utm = new Query();
		query27utm.put("staffId", staffId);
		query27utm.put("showOn", showOn);
		query27utm.put("queryId", "51565832709d-3243-2d2d-ad71-4a9540d406ef");
		query27utm.put("campaignid", "5156");
		query27utm.put("type", "BATCH");
		List<WxworkTaskVo> utm27list=wxworkNccTaskService.taskListByStaffId(query27utm);
		
		Query query55nutm = new Query();
		query55nutm.put("staffId", staffId);
		query55nutm.put("showOn", showOn);
		query55nutm.put("queryId", "5157b3383862-bbc8-1ef6-a8e9-2f00878c4d5d");
		query55nutm.put("campaignid", "5157");
		query55nutm.put("type", "BATCH");
		List<WxworkTaskVo> nutm55list=wxworkNccTaskService.taskListByStaffId(query55nutm);
		
		Query query55utm = new Query();
		query55utm.put("staffId", staffId);
		query55utm.put("showOn", showOn);
		query55utm.put("queryId", "5156b3383862-bbc8-1ef6-a8e9-2f00878c4d5d");
		query55utm.put("campaignid", "5156");
		query55utm.put("type", "BATCH");
		List<WxworkTaskVo> utm55list=wxworkNccTaskService.taskListByStaffId(query55utm);
		map.put("nutm27", nutm27list); 
		map.put("nutm55", nutm55list); 
		map.put("utm27", utm27list);
		map.put("utm55", utm55list);
		return Result.success(map);
	}
}
